<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *返现管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择一条返现！','error');
	}
}
$return=array();
$tabs=array(
	array('action'=>'jy_aligroup/cashback/list/','title'=>'返现总列表'),
	array('action'=>'jy_aligroup/cashback/list/status=0','title'=>'未完成返现列表'),
	array('action'=>'jy_aligroup/cashback/list/status=1','title'=>'已完成返现列表'),
	array('action'=>'jy_aligroup/cashback/list/status=2','title'=>'失败返现列表'),

	
);
$return['status']=array('0'=>'待退款','1'=>'已退款','2'=>'退款失败');
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','orderid','teamid','money','status','type','createtime');
	$list=pdo_getslice('jy_aligroup_groups_cashback',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$order=pdo_get('jy_aligroup_groups_order',array('id'=>$item['orderid'],'uniacid'=>$_SESSION['uniacid']));
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['corpstitle']=pdo_getcolumn('jy_aligroup_groups_corps',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['cid']),'title');
			$item['headsid']=pdo_getcolumn('jy_aligroup_groups_team',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['teamid']),'headsid');
			$item['teammember']=cfc('core')->member_show($item['headsid']);
			if(!$item['type']){
				$item['type']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$item['orderid']),'pay_type');
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：</br>1、这里显示拼团失败的退款！','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'参团人','type'=>'member'),
			array('name'=>'corpstitle','title'=>'战队名称','type'=>'string'),
			array('name'=>'teammember','title'=>'团长','type'=>'member'),
			array('name'=>'money','title'=>'金额','type'=>'string'),
			array('name'=>'type','title'=>'支付类型','type'=>'radio','options'=>'pay_type'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),'action'=>array(
			array('type'=>'modal','op'=>'pay','title'=>'打款','show'=>array('status'=>0)),
		)),
		'search'=>array(
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status','width'=>15),
		)
	);
	$return['pay_type']=get_pay_types();
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='pay'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$type=$post['type'];
		$status=intval($post['status']);
		if(!$status){
			exi('请先选择是否审核通过！','error');
		}
		if($status==1){
			$this->model('order')->refund_out($id);
			exi('打款成功！','','list');
		}else{
			if(!$post['remark']){
				exi('请填写审核不通过原因！','error');
			}
			pdo_update('jy_aligroup_groups_cashback',array('status'=>2,'remark'=>trim($post['remark'])),array('id'=>$id));
			exi('操作成功！','','list');
		}
	}
	$item=pdo_get('jy_aligroup_groups_cashback',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定结算单不存在！','error');
	}
	if($item['status']==1){
		exi('该退款已完成，请勿重复操作~','error');
	}
	if(!$item['type']){
		$item['type']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$item['orderid']),'pay_type');
	}
	$item['typetitle']=get_pay_types()[$item['type']];
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'打款',
		'edit'=>array(
			array('type'=>'tip','title'=>'【温馨提示】：如果打款会员是<red>小程序</red>会员，必须选择打款《小程序》，否则可能会打款不成功!默认打款使用的是公众号！'),
			array('type'=>'string','name'=>'money','title'=>'打款金额','read'=>true),
			array('name'=>'typetitle','title'=>'打款方式','type'=>'string','read'=>true),
			array('type'=>'radio','name'=>'status','title'=>'审核','options'=>'statuss'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'string','show'=>array('status'=>2)),
		),
	);
	$return['statuss']=array(
		'1'=>'审核通过并打款',
		'2'=>'审核不通过',
	);
	$return['wxapps']=pdo_getall('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	
	exi($return);
}